﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obligatorio2013.Modelo;
using Obligatorio2013.Clases;

namespace Obligatorio2013.Pages
{
    public partial class EditUserForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((bool)Session["logged"] == true)
            {
                Users u = null;
                if (!Page.IsPostBack)
                {

                    this.showUserRoles();
                    if (Session["controller"] == null)
                    {
                        Session["controller"] = Sistema.Instance;
                    }
                    string usrEmail = (string)(Session["editUsr"]);
                    Sistema system = Sistema.Instance;
                    u = system.getUserById(usrEmail);

                    if (u != null)
                    {
                        this.txtName.Text = u.UserName;
                        this.txtLastName.Text = u.UserLastName;
                        this.txtAddress.Text = u.UserAddress;
                        this.txtLocation.Text = u.UserAddressLocaltion;
                        this.txtEmail.Text = u.UserEmail;
                        this.txtPhone.Text = u.UserPhone;
                        this.txtUser.Text = u.User;
                        this.DropDownList1.SelectedValue = u.UserRole;

                        if (!u.UserActive)
                        {
                            this.chkSuspendido.Checked = true;
                            this.CalendarExtender1.SelectedDate = u.UserInactiveUntil;
                        }
                    }
                }
                else
                {
                    string usrEmail = (string)(Session["editUsr"]);
                    Sistema system = Sistema.Instance;
                    u = system.getUserById(usrEmail);
                    if (u.UserActive)
                    {
                        this.chkSuspendido.Checked = false;
                        this.CalendarExtender1.Enabled = false;
                    }
                    else
                    {
                        this.chkSuspendido.Checked = true;
                        this.CalendarExtender1.Enabled = true;
                        this.CalendarExtender1.SelectedDate = u.UserInactiveUntil;
                    }
                }
            }
            else
            {
                Response.Redirect("~/Pages/Private/LoginForm.aspx");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/UserList.aspx", true);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Sistema system = Sistema.Instance;
            bool ret = system.validateUserInfo(this.txtName.Text, this.txtLastName.Text, this.txtAddress.Text, this.txtLocation.Text, this.txtPhone.Text, this.txtEmail.Text,
                this.txtUser.Text, this.txtPass.Text);

            if (ret)
            {
                if (this.chkSuspendido.Checked)
                {
                    system.updateUser(this.txtName.Text, this.txtLastName.Text, this.txtAddress.Text, this.txtLocation.Text, this.txtPhone.Text, this.txtEmail.Text,
                                       this.txtUser.Text, this.txtPass.Text, this.DropDownList1.SelectedValue, this.chkSuspendido.Checked, this.CalendarExtender1.SelectedDate.Value);
                }
                else
                {
                    bool result = system.updateUser(this.txtName.Text, this.txtLastName.Text, this.txtAddress.Text, this.txtLocation.Text, this.txtPhone.Text, this.txtEmail.Text,
                    this.txtUser.Text, this.txtPass.Text, this.DropDownList1.SelectedValue, this.chkSuspendido.Checked, DateTime.MinValue);

                    if (result)
                    {
                        Response.Redirect("~/Pages/UserList.aspx", true);
                    }
                }
            }

        }

        protected void chkSuspendido_CheckedChanged(object sender, EventArgs e)
        {

            if (this.chkSuspendido.Checked)
            {
                //this.CalendarExtender1.Visible = true;    
            }

        }


        private void showUserRoles()
        {
            Array roles = Enum.GetValues(typeof(Users.roles));

            foreach (int key in roles)
            {
                string desc = Enum.GetName(typeof(Users.roles), key);
                this.DropDownList1.Items.Add(new ListItem(desc, key.ToString()));
            }
        }
    }
}